//
//  PKPlayViewController.m
//  PianKe
//
//  Created by mingtao on 15/10/6.
//  Copyright (c) 2015年 mingtao. All rights reserved.
//

#import "PKPlayViewController.h"
#import "PKPlayListTableViewController.h"
#import "PKPlaylistSuroView.h"
#import "PKBroadcastView.h"
#import "PKPlayerManager.h"
#import "PKPlarview.h"
#import "PKPlarRadioViewController.h"
#import "PKRequestManager.h"
#import "PKRadiomDetailModel.h"
#import "PKRadiomodelMoreting.h"
#import "PKPlarview.h"
#import "PKRadioDetail.h"

#import "LORefresh.h"


#import <UIButton+WebCache.h>


@interface PKPlayViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)PKRadiomDetailModel *palRadiom;
@property(nonatomic,strong)UIScrollView *mainScrolview;

@property(nonatomic,strong)PKPlayListTableViewController *listTableviewController;

@property(nonatomic,strong)PKBroadcastView *MusicView;


@property(nonatomic,strong)PKPlaylistSuroView *SuroView;

@property (nonatomic,strong) UIWebView *webView;

@property(nonatomic,strong)PKPlarview *plarview;

@property(nonatomic,strong)NSMutableArray *nsmuArray;

@property(nonatomic,strong)NSMutableArray *NsmuArrayPalr;
@property(nonatomic,strong)PKPlarview *plarviewtxt;

@property(nonatomic,strong)NSMutableArray *redidPalArray;

@property(nonatomic,assign)BOOL isBol;

@property(nonatomic,strong)NSMutableArray *NewArr;





@end

@implementation PKPlayViewController

-(NSMutableArray *)NewArr
{
    if (_NewArr == nil) {
        _NewArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _NewArr;
}

-(NSMutableArray *)redidPalArray
{
    if (_redidPalArray == nil) {
        _redidPalArray = [NSMutableArray arrayWithCapacity:0];
    }
    
    return _redidPalArray;

}

-(NSMutableArray*)NsmuArrayPalr
{
    if (_NsmuArrayPalr == nil) {
        _NsmuArrayPalr = [NSMutableArray arrayWithCapacity:0];
    }
    return _NsmuArrayPalr;

}

-(NSMutableArray *)nsmuArray
{
    if (_nsmuArray==nil) {
        _nsmuArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _nsmuArray;
}

-(void)requestr:(NSString*)tingid
{
    NSMutableDictionary *plarDic=[NSMutableDictionary dictionary];
    plarDic[@"auth"]=@"";
    plarDic[@"client"]=@"1";
    plarDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    plarDic[@"tingid"] = tingid;
    plarDic[@"version"] = @"3.0.4";
    
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/ting/info" parDic:plarDic finish:^(NSData *data) {
        
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        //NSArray *palrarray=dic[@"data"];
//        NSLog(@"uuuuuuuuuuuuuuuuuuu%@",dic);
        _palRadiom=[[PKRadiomDetailModel alloc]init];
        [_palRadiom setValuesForKeysWithDictionary:dic[@"data"]];
        
        _SuroView.DetaiMode=_palRadiom;
        
        [self loadWebPageWithURLString:_palRadiom.playInfo.webview_url];
        
        
        
//        [self.nsmuArray addObject:palRadiom];
//        NSArray *array=dic[@"data"][@"moreting"];
//        
//        for (NSDictionary *dicKey in array) {
//            PKRadiomodelMoreting *parRadimoel=[[PKRadiomodelMoreting alloc]init];
//            //moreting
//            
//            [parRadimoel setValuesForKeysWithDictionary:dicKey];
//            
//            [self.NewArr addObject:parRadimoel];
//            
//        }
        
        
        for (int i = 0 ; i<self.NsmuArrayPalr.count; i++) {
//            NSString *musicUrl=((PKRadioDetail *)self.NsDetailArray[i]).musicUrl;
//            [self.Nsarrays addObject:musicUrl];
            //ReadimoArray
            NSString *ImagemusURL=((PKRadiomodelMoreting *)self.NsmuArrayPalr[i]).coverimg;
            
            [self.redidPalArray addObject:ImagemusURL];
           
            
        }
        
        _plarview=[[PKPlarview alloc]initWithFrame:CGRectMake(WIHES*3, 0, WIHES, HHIHEI)];
        
        [_plarview.ButAll addTarget:self action:@selector(plarviewButAll:) forControlEvents:UIControlEventTouchUpInside];
        [_plarview.worksModelView1.button sd_setImageWithURL:[NSURL URLWithString:_redidPalArray[0]] forState:(UIControlStateNormal)];
        
        [_plarview.worksModelView2.button sd_setImageWithURL:[NSURL URLWithString:_redidPalArray[1]] forState:(UIControlStateNormal)];
        
        
        [_plarview.worksModelView3.button sd_setImageWithURL:[NSURL URLWithString:_redidPalArray[2]] forState:(UIControlStateNormal)];
        
        
        [_plarview.worksModelView4.button sd_setImageWithURL:[NSURL URLWithString:_redidPalArray[3]] forState:(UIControlStateNormal)];
        
        
        [_plarview.worksModelView5.button sd_setImageWithURL:[NSURL URLWithString:_redidPalArray[4]] forState:(UIControlStateNormal)];
        
        
        [_plarview.worksModelView6.button sd_setImageWithURL:[NSURL URLWithString:_redidPalArray[5]] forState:(UIControlStateNormal)];
        
        [_mainScrolview addSubview:_plarview];
        
    } error:^(NSError *error) {
        NSLog(@"查看全部电台%@",error);
    }];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    //_radioDetailModel.tingid
    [self requestr:_radioDetailModel.tingid];
    
    _mainScrolview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 70, WIHES, HHIHEI-140)];
    _mainScrolview.contentSize = CGSizeMake(WIHES*4, HHIHEI-140);
    _mainScrolview.backgroundColor = [UIColor whiteColor];
    _mainScrolview.pagingEnabled = YES;
    _mainScrolview.delegate=self;
    
    //设置偏移量
    _mainScrolview.contentOffset = CGPointMake(WIHES, 0);
    
 
#pragma mark 通知
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changelistindext:) name:@"listTableview" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changgeArr:) name:@"changsha" object:@"pkpalaylist"];
    
#pragma mark   页面图片显示
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changgeImage:) name:@"changgelist" object:@"parllist"];
    
    
    PKPlayerManager *playerManager =[PKPlayerManager shareManager];
    
    [playerManager setManagerMusicArray:self.arr];
    
    [playerManager playWithIndex:self.Subscript];
    //self.Subscript

    
    //_MusicView
    
    _listTableviewController = [[PKPlayListTableViewController alloc]init];
    
    _listTableviewController.dataSourceArray = self.datalistSourceArray;
    
    _listTableviewController.tableView.frame = CGRectMake(0, 0, WIHES, HHIHEI-140);
    
    
    _SuroView = [[PKPlaylistSuroView alloc]initWithFrame:CGRectMake(WIHES, 0, WIHES, HHIHEI-70)];
    
    //Slider 移动值
    [_SuroView.Slider addTarget:self action:@selector(Slidervalue:) forControlEvents:(UIControlEventValueChanged)];
    
    [_mainScrolview addSubview:_SuroView];
#pragma 设置手势
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapSlider:)];
    
    [_SuroView.Slider addGestureRecognizer:tap];
    
    
    __block PKPlayViewController *texts=self;
    
    _listTableviewController.parDetailBlock = ^(NSInteger index)
    {
        NSString *str=texts.arrindex[index];
        
       // [texts requestr:texts.arrindex[index]];
        [texts requestr:str];

    };
    
    
    
#pragma Block传值
    //parDetailBlock
//    _listTableviewController.parDetailBlock=^(NSInteger index)
//    {
//    
//        [self requestr:];
//    
//    };
    
    
    //UIWebView 网页跳转
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(WIHES*2, 0, WIHES, HHIHEI-140)];
    
    [self loadWebPageWithURLString:self.radioDetailModel.playInfo.webview_url];
    
    [_mainScrolview addSubview:_webView];
    
    
    [_mainScrolview addSubview:_listTableviewController.tableView];
    
    [self.view addSubview: _mainScrolview];
    
    
    


    
   //
    //PKPlarview

    
   // radioDetailModel
    
    
    _MusicView=[[PKBroadcastView alloc]initWithFrame:CGRectMake(0, HHIHEI-70, WIHES, 70)];
    
    //播放
    [_MusicView.ButPlayPause addTarget:self action:@selector(PlayPauseBt:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //上一曲
    [_MusicView.ButOnsong addTarget:self action:@selector(OnsongBut:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //下一曲
    [_MusicView.ButNextsong addTarget:self action:@selector(NextsongBut:) forControlEvents:(UIControlEventTouchUpInside)];
    //lableTimeZ
    
           // _lableTimeZ.text =[NSString stringWithFormat:@"%lu",_mananger.finishTime];
    

    
   // _lableTimeZ.text = [NSString stringWithFormat:@"%lu",playerManager.finishTime];
    //playerManager
    
    //  _mananger = [PKPlayerManager shareManager];
    
    
   // [self.view addSubview:_lableTimeZ];

    
    
    //定时器
    NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
    
    _MusicView.BroadcastModel = self.radioDetailModel;
    [self.view addSubview:_MusicView];
    

    
    
    
}






#pragma mark 数组
-(void)changgeImage:(NSNotification *)imageArr
{
     self.arrindex = imageArr.userInfo[@"Nsarrays"];


}

#pragma mark 通知
-(void)changgeArr:(NSNotification*) NsnoArr
{
    
    PKPlayerManager *playerManager =[PKPlayerManager shareManager];
    
    [playerManager setManagerMusicArray:self.arr];
    
    //self.Subscript
    
    [playerManager playWithIndex:((NSIndexPath *)NsnoArr.userInfo[@"indexpath"]).row];
}

#pragma  mark tap手势
-(void)tapSlider:(UITapGestureRecognizer *)tap
{
    CGFloat value=[tap locationInView:_SuroView.Slider].x;
    
    CGFloat valueCurr=value/_SuroView.Slider.frame.size.width;
    
    NSUInteger timer=valueCurr *([PKPlayerManager shareManager]).finishTime;
    [[PKPlayerManager shareManager] seekToTime:timer];
    
}
-(void)plarviewButAll:(UIButton *)butAll
{
    PKPlarRadioViewController *plarView=[[PKPlarRadioViewController alloc]init];
    
    [self.navigationController pushViewController:plarView animated:YES];

}

// 加载网页
- (void)loadWebPageWithURLString:(NSString *)URLstring
{
    // 加载请求即可
    NSURL *url = [NSURL URLWithString:URLstring];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
}

//定时器
-(void)timeChange
{
    PKPlayerManager *playerManager =[PKPlayerManager shareManager];
      _SuroView.lableTimeZ.text =[self changtime:(playerManager.finishTime-playerManager.currentTime)];
    
    if ([[self changtime:(playerManager.finishTime-playerManager.currentTime)] isEqualToString:@"00:01"]) {
        
        [[PKPlayerManager shareManager]nextMusic];

    }

  //  _SuroView.lableTimeZ.text =[self changtime:(playerManager.finishTime-playerManager.currentTime)];
    _SuroView.Slider.value=  _SuroView.Slider.maximumValue *((CGFloat)playerManager.currentTime /(CGFloat)playerManager.finishTime);
    
}

-(void)Slidervalue:(UISlider*)selider
{
    CGFloat scale=  selider.value/selider.maximumValue;
    NSUInteger timer=scale *([PKPlayerManager shareManager]).finishTime;
    [[PKPlayerManager shareManager] seekToTime:timer];
    
}


//下一曲
-(void)NextsongBut:(UIButton *)but
{

    [[PKPlayerManager shareManager]nextMusic];
}

//上一曲
-(void)OnsongBut:(UIButton*)but
{
    [[PKPlayerManager shareManager]lastMusic];

}


//播放
-(void)PlayPauseBt:(UIButton *)but
{
    // [_ButPlayPause setImage:[UIImage imageNamed:@"iconfont-zanting"] forState:(UIControlStateNormal)];
    if (_isBol==YES) {
        [_MusicView.ButPlayPause setImage:[UIImage imageNamed:@"iconfont-zanting"] forState:(UIControlStateNormal)];
        _isBol=NO;
    }else
    {
        [_MusicView.ButPlayPause setImage:[UIImage imageNamed:@"iconfont-yinlebofang"] forState:(UIControlStateNormal)];
        _isBol = YES;
    }
    
    [[PKPlayerManager shareManager]playAndPause];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


-(NSString *)changtime:(NSInteger)number
{
    NSInteger a,b,c,d;
    
    a=number/60/10;
    b=number/60%10;
    c=number%60/10;
    d=number%60%10;
    
    return [NSString stringWithFormat:@"%ld%ld:%ld%ld",a,b,c,d];

}

@end
